<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/14 0014
 * Time: 16:32
 */
namespace app\api\controller;

use app\admin\model\Integralproducts;
use app\admin\model\Productdetail;
use app\common\BaseHelper;
use app\common\model\Orderdistribution;
use app\common\model\Orderproductdetail;
use app\common\model\Productspecgroups;
use app\common\model\User;


class Orders extends BaseController {



    public function createIntegralpProducts(){
        $token = new \app\api\service\Token();
        $uid = $token->getCurrentUid();
        $userModel = new User();
        //获取当前用户积分
        $user = $userModel->findCurrentUser($uid);
        $integral = $user->integral;

        $storeId = input("post.storeId");
        $productId = input("post.productId");
        $total_count = input('post.total_count');
        $specid = input('post.specid');
        $deliverAddress = input('post.deliverAddress');
        $linkType = input('post.linkType');
        $deliverUserName = input('post.userName');
        $deliverUserPhone = input('post.userPhone');
        $deliverMethod = input('post.deliverMethod');
        $productspecgroupsModel = new Productspecgroups();
        $IntegralproductsModel = new Integralproducts();
        $data['storeId'] = $storeId;
        $productModel = new \app\admin\model\Product();
        if(!$storeId){
            return show(config('status.ERROR_STATUS'), "商品门店id，不能为空 ",0);
        }
        if(!$productId){
            return show(config('status.ERROR_STATUS'), "商品不能为空",0);
        }

        $data['productId'] = $productId;
        $storeModel = new \app\admin\model\Store();
        $store = $storeModel->findStore($storeId);
        if(!$store){
            return show(config('status.ERROR_STATUS'), "用户积分余额不足",0);

        }
        $content = input("content");
     //   $aeceivingAddress = $address->provincName.$address->cityName.$address->districtName.$address->address;
        //获取积分商品所需价格
        $integralProducts = $IntegralproductsModel->findIntegralproducts($data);
        if(isset($integralProducts->getStoreProduct) && !empty($integralProducts->getStoreProduct)){
            $product = $integralProducts->getStoreProduct;
            if($product && !empty($product)){
                $changeIntegral = $product->integralPrice;
                if($integral<$changeIntegral){
                    return show(config('status.SUCCESS_STATUS'), "用户积分余额不足",0);
                }
            }
        }

        $orderService = new \app\api\service\Order();
        $orderNo = $orderService->makeOrderNo();
        if(!$total_count || $total_count<=0 ){
            return show(config('status.SUCCESS_STATUS'), "商品数量不能为空或和小于等于0",0);
        }
        if(!$linkType){
            return show(config('status.SUCCESS_STATUS'), "订单应用类型不能为空",0);
        }
        if(isset($changeIntegral) && $changeIntegral){
            $orderModel = new \app\common\model\Order();
            //生成积分商品订单
            $dataParam['id'] =  BaseHelper::getUUID();
            $dataParam['storeId'] = $storeId;
            $dataParam['userName'] = $deliverUserName;
            $dataParam['userPhone'] = $deliverUserPhone;
            $dataParam['integral'] =  $changeIntegral;//商品积分
            $dataParam['storeId'] =  $storeId;//门店id
            $dataParam['orderNO'] = $orderNo;
            $dataParam['create_time'] = time();
            $dataParam['createUser'] = $uid;
            $dataParam['total_count'] =  $total_count;         //商品总数
            $dataParam['status'] = $orderModel::STATUS_PENDING_DELIVERY;       //待发货状态
            $dataParam['tenantId'] = $store->tenantId;
          //  $dataParam['userAddressId'] =$addressId;       //买家地址id
            $dataParam['content'] = $content;                  //买家留言
            $dataParam['deliverAddress'] = $deliverAddress;              //收货地址
            $dataParam['linkType'] = $linkType;              //应用类型   1=拼团  2=积分换购
            $dataParam['type'] = $orderModel::TYPE_0;                    //类型  0=线上订单 1=线下订单
            $dataParam['deliverMethod'] = $deliverMethod;  //0 物流 1商家配送 2达达配送 3 到店自取
            $orderResult = $orderModel->addIntegralOrder($dataParam);
            //更新用户积分
            User::where('id',$uid)->setDec('integral',$changeIntegral);
            if($orderResult){
                $distributionTime = input('distributionTime');
                $tenantDistributionId= input('tenantDistributionId');
                if($distributionTime && !empty($distributionTime)){
                    $orderDistribution = new Orderdistribution();
                    $orderDistributionData['id'] = get_uniqid_id();
                    $orderDistributionData['distributionTime'] = $distributionTime;
                    $orderDistributionData['tenantDistributionId'] = $tenantDistributionId;
                    $orderDistributionData['orderId'] = $dataParam['id'];
                    $orderDistributionData['create_time'] = time();
                    $distribution = $orderDistribution->addOrderDistibution($orderDistributionData);
                    if(!$distribution){
                        return show(config('status.ERROR_STATUS'), "预定配送时间！失败", array(
                            'distribution'=>$distribution
                        ));
                    }
                }
                if(isset($product) && !empty($product)){
                    $productDetailDate['id'] = get_uniqid_id();
                    $productDetailDate['orderId'] = $dataParam['id'];
                    $productDetailDate['productName'] = $product->title;
                    $productDetailDate['productId'] = $product->id;
                    $productDetailDate['count']=$total_count;
                    $productDetailDate['productImg']=$product->imgUrl;
                    $productDetailDate['userId'] = $uid;
                    $productDetailDate['tenantId'] = $store->tenantId;
                    $productDetailDate['oldStatus'] = $orderModel::STATUS_PENDING_DELIVERY;
                    $productDetailDate['create_time'] = time();
                    if(isset($specid) && $specid){
                        $productSpec = $productspecgroupsModel->getSpec($specid);
                        $productDetailDate['productSpec'] = $productSpec->specType1;
                        $productDetailDate['productSpec1'] = $productSpec->specType2;
                        $productDetailDate['productSpec2'] = $productSpec->specType3;
                    }
                    $productDetailModel = new Orderproductdetail();
                    $insterProductDetail = $productDetailModel->insterProductDetail($productDetailDate);
                    if(!$insterProductDetail){
                        return show(config('status.ERROR_STATUS'), "订单商品详情报错失败！", array(
                            'productDetaiId'=>""
                        ));
                    }else{
                        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, array(
                            'orderId'=>$dataParam['id']
                        ));
                    }
                }else{
                    return show(config('status.ERROR_STATUS'), "订单商品详情报错失败！", array(
                        'orderId'=>""
                    ));
                }

            }else{
                return show(config('status.SUCCESS_STATUS'), "数据保存失败", 0);
            }
        }
    }




}